package com.kintreda.ieco_server.module.order.web;

import com.kintreda.ieco_server.bean.goods.Items;
import com.kintreda.ieco_server.bean.order.UsageRecord;
import com.kintreda.ieco_server.core.web.BaseController;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.order.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.Transient;
import java.util.List;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.web
 * @Author ZhongYao_H
 * @Date 2019/4/29:15:00
 * @Year 2019 Month 04 Day 29
 * @Date_zh 2019 年 四月 星期一
 */
@Controller
@RequestMapping("/orderclose")
public class OrderCloseController extends BaseController {
    @Autowired
    private IUsageRecordService usageRecordService;
    @Autowired
    private IRecycleRecordService recycleRecordService;
    @Autowired
    private IRegenerantService regenerantService;
    @Autowired
    private IWasteService wasteService;
    @Autowired
    private IUnsuitableService unsuitableService;


    @RequestMapping("/list")
    public ModelAndView list(){
        return  new ModelAndView("order/order-close/order-list");
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public List query(@RequestParam(value = "page",defaultValue="1") Integer page,
                      @RequestParam(value = "rows",defaultValue="10") Integer rows,String code){
        if (StringUtils.isNotBlank(code)){
            return recycleRecordService.getListByCode(code);
        }

        return recycleRecordService.getAll();
    }

    @RequestMapping("/det")
    public ModelAndView details(String id){
        ModelAndView andView = new ModelAndView("order/order-close/order-details");
        andView.addObject("recycleRecord",recycleRecordService.get(id));
        andView.addObject("wastes",wasteService.getWasteByRecyId(id));
        andView.addObject("unsuitable",unsuitableService.getByRecyId(id));
        return andView;
    }


    /**
     *
     * 结算废品
     * @return
     */
    @RequestMapping("/close")
    public ModelAndView close(String id){
        ModelAndView andView = new ModelAndView("order/order-add");
        List<Items> types = regenerantService.getType("1");
        for (Items type : types) {
            List<Items> goods;
            switch(type.getTyepC()){

                case "0":
                    andView.addObject("type0",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child0",goods);
                    break;
                case "1":
                    andView.addObject("type1",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child1",goods);
                    break;
                case "2":
                    andView.addObject("type2",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child2",goods);
                    break;
                case "3":
                    andView.addObject("type3",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child3",goods);
                    break;
                case "4":
                    andView.addObject("type4",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child4",goods);
                    break;
                case "5":
                    andView.addObject("type5",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child5",goods);
                    break;
                case "6":
                    andView.addObject("type6",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child6",goods);
                    break;
                case "7":
                    andView.addObject("type7",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child7",goods);
                    break;
                case "8":
                    andView.addObject("type8",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child8",goods);
                    break;
                case "9":
                    andView.addObject("type9",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child9",goods);
                    break;
                case "10":
                    andView.addObject("type10",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child10",goods);
                    break;
                case "11":
                    andView.addObject("type11",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child11",goods);
                    break;
                case "12":
                    andView.addObject("type12",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child12",goods);
                    break;
                case "13":
                    andView.addObject("type13",type);
                    goods = regenerantService.getByTypeId(type.getId());
                    andView.addObject("child13",goods);
                    break;
                default:
                    break;
            }
        }
//        andView.addObject("types",types);
        andView.addObject("id",id);
        return  andView;
    }

    /**
     *
     * 结算废品
     * @return
     */
    @RequestMapping(value = "/close",method = RequestMethod.POST)
    @ResponseBody
    @Transient
    public synchronized  Object close(String[] type,String[] ids,String[] num,String id,String differentiate,Integer rate,String explain){
        R close = recycleRecordService.close(type,ids, num, id,differentiate,rate,explain);
        String code = close.get("code").toString();
        if ("0".equals(code)){
            return close;
        }else {
            return close;
        }
    }

    @RequestMapping(value = "/get",method = RequestMethod.POST)
    @ResponseBody
    public R getUsageRecord(String code){
        code = code.toUpperCase();
        UsageRecord usageRecord = usageRecordService.getByQrcode(code, "2");
        if (usageRecord!=null){
            R r = R.ok();
            r.put("body",usageRecord);
            return  r;
        }
        return R.error();
    }





}
